# -*- coding: utf-8 -*-
"""
@project: AutoTesting_pulsar
@author: chenhh
@file: Request.py
@ide: PyCharm
@time: 2021/1/25
"""

import requests
from Common.Extract_data import Extractdata
from Common.Read_xls import Readxlsdata
from Config.Config import Config
from Common.Session import Session
import json


class RequestInterface(object):

	def __is_json(self, param):
		try:
			json_object = json.loads(param)
		except ValueError as e:
			return False
		return True

	# 定义处理不同类型的请求参数、包含字典、字符串、空值
	def __new_param(self, param):
		try:
			if isinstance(param, str) and self.__is_json(param):
				new_param = eval(param)
				#new_param = param
			elif param == None:
				new_param = ''
			else:
				new_param = param
		except Exception as error:
			new_param = ''
		return new_param

	# post请求，参数在body中

	def __http_post(self, interface_url, interface_param):
		'''
            interface_url:接口地址
            interface_param：接口请求参数
            return 返回字典形式结果
        '''
		try:
			if interface_url != '':
				config = Config()
				a = config.get_conf('test_host', 'test_url')
				get_url = a + interface_url
				# print(get_url)
				temp_interface_param = self.__new_param(interface_param)
				ss = Session()
				cookie = ss.get_session()
				print(cookie)
				response = requests.post(url=get_url, json=temp_interface_param, cookies=cookie, verify=False,
										timeout=10)
				print(response.content)
				print(response.headers,response.url)
				# print(response.text)
				if response.status_code == 200:
					result = response.text
				else:
					result = {'code': '2004', 'message': '接口返回状态错误', 'data': []}
			elif interface_url == '':
				result = {'code': '2002', 'message': '接口地址参数为空', 'data': []}
			else:
				result = {'code': '2003', 'message': '接口地址错误', 'data': []}
		except Exception as e:
			result = {'code': '9999', 'message': '系统异常', 'data': []}
		return result

	# get请求,参数在接口地址后面

	def __http_get(self, interface_url, interface_param):
		'''
            interface_url:接口地址
            interface_param：接口请求参数
            return 返回字典形式结果
        '''

		try:
			if interface_url != '':
				config = Config()
				url = config.get_conf('test_host', 'test_url')
				get_url = url + interface_url
				temp_interface_param = self.__new_param(interface_param)

				# print(temp_interface_param
				# print(get_url)
				ss = Session()
				cookie = ss.get_session()
				response = requests.get(url=get_url, params=temp_interface_param, cookies=cookie)
				# print(response.text)
				if response.status_code == 200:
					result = response.text
				else:
					result = {'code': '3004', 'message': '接口返回状态错误', 'data': []}
			elif interface_url == '':
				result = {'code': '3002', 'message': '接口地址参数为空', 'data': []}
			else:
				result = {'code': '3003', 'message': '接口地址错误', 'data': []}
		except Exception as e:
			result = {'code': '9999', 'message': '系统异常', 'data': []}
		return result

	# 统一处理http请求

	def http_request(self, interface_url, interface_param, request_type):
		'''
            interface_url:接口地址
            interface_param：接口请求参数
            request_type:请求类型
            return 返回字典形式结果
        '''
		try:
			if  interface_url != '' and interface_param != '' and request_type != '':
				if request_type == 'get' or request_type == 'GET':
					result = self.__http_get(interface_url, interface_param)
				elif request_type == 'post' or request_type == 'POST':
					result = self.__http_post(interface_url, interface_param)
				else:
					result = {'code': '1000', 'message': '请求类型错误', 'data': request_type}
			else:
				result = {'code': '8888', 'message': 'url / param / request_type参数错误', 'data': []}
		except Exception as e:
			result = {'code': '9999', 'message': '系统异常', 'data': []}
		return result


'''
if __name__ == '__main__':

	res = Readxlsdata(flie_name=r"\Casedata\testdata.xlsx")
	res1 = res.get_casedata()
	d = res1[0][0][3]
	f = res1[0][0][7]
	m = res1[0][0][5]
	td = res1[0][0][12]
	print(f)
	res = RequestInterface()
	response = res.http_request(interface_url=d, interface_param=f, request_type=m)
	aa = Extractdata(td)
	#data = json.loads(response)
	#c = data["data"][id]


	ss = Session()
	cookie = ss.get_session()

	a = {"resource_type": "MaxCompute", "table_name": "dim_tpchcustomer", "module": "\u6570\u636e\u5efa\u6a21"}

	response = requests.post(url='https://bigdata-test.mypaas.com.cn/api/modeling/data_quality/monitor/add', json=a,cookies=cookie)
	print(response.text)
'''